package com.hs.dnms.action.search;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.core.action.BaseAction;
import com.hs.core.cache.GlobalParamCache;
import com.hs.core.db.DBMgr;
import com.hs.core.db.JdbcTemplateUtil;
import com.hs.core.db.QueryPage;
import com.hs.dnms.model.DnmsFile;
import com.hs.dnms.service.IDataService;
@Controller  
public class MutiController  extends BaseAction {
    /**
	 * 
	 */
	private static final long serialVersionUID = -354434172898440670L;
	private static final Logger logger = Logger.getLogger(MutiController.class);
	private IDataService dataService;
    @Autowired  
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}
//	 
//	 
	 /**
	  * 检索首页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/muti/mutindex.do")   
     public ModelAndView mutindex(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
         
 		 return new ModelAndView("muti/counsum",modelMap);
		 
	 } 
	 /**
	  * 检索结果页面
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/muti/doSearch.do")   
     public ModelAndView doSearch(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		  JdbcTemplateUtil util = JdbcTemplateUtil.getInst();
		  GlobalParamCache cache = GlobalParamCache.getInst();
		   
		//排序
		 StringBuffer allfullsql = new StringBuffer(); 
		 String colname = request.getParameter("colname");
		 String keyword = request.getParameter("keyword");
		 String onesql = request.getParameter("onesql"); 
 		 String orderName = request.getParameter("orderName");
		 String orderNum = request.getParameter("orderNum");
		 String viewtype = request.getParameter("viewtype");
		 String datarange = request.getParameter("datarange");
		 String perPageCount = request.getParameter("perPageCount");
		 if(StringUtils.isEmpty(datarange)){
			 datarange = "0";
		 }
		 if(StringUtils.isEmpty(viewtype)){
			 viewtype = "1";
		 }
		 StringBuffer orderByStr = new StringBuffer(" order by ");
		 if(StringUtils.isNotBlank(orderName)){
		    	if("1".equals(orderNum)){
		    		orderByStr.append(orderName).append(" asc");
		    	}
		    	if("2".equals(orderNum)){
		    		orderByStr.append(orderName).append(" desc");
		    	}
		    }else{
		    	orderByStr.append("title").append(" desc");
		    	orderName = "title";
		    	orderNum = "2";
		    }
		 request.setAttribute("orderName", orderName); 
		 request.setAttribute("orderNum", orderNum); 
		 request.setAttribute("viewtype", viewtype);  
		 String sqlold = "";
		 if("0".equals(datarange) || "2".equals(datarange)){
			 sqlold = generaterSQLold(colname,keyword);
		 }
		 
		 String sqlxiandai = "";
		 if("0".equals(datarange) || "1".equals(datarange)){
			 sqlxiandai = generaterSQL(colname,keyword);
		 }
		 allfullsql.append("select * from (");
 		 if( StringUtils.isNotEmpty(sqlxiandai)&&StringUtils.isEmpty(sqlold)){
			 allfullsql.append(sqlxiandai);
		 }else if(StringUtils.isNotEmpty(sqlxiandai)&&StringUtils.isNotEmpty(sqlold)){ 
			 allfullsql.append(sqlxiandai);
			 allfullsql.append(" union all ");
			 allfullsql.append(sqlold);
		 }else if(StringUtils.isEmpty(sqlxiandai)&&StringUtils.isNotEmpty(sqlold)){
			 allfullsql.append(sqlold);
		 }
 		allfullsql.append(")  filetab");
		 if(StringUtils.isNotBlank(onesql)){
			 onesql = "select id from(" + onesql + ") ";
			 allfullsql.append(" where  filetab.id in (");
			 allfullsql.append(onesql);
			 allfullsql.append(")");
		 }
		 allfullsql.append(orderByStr);
		 
		 System.out.println("allfullsql::" + allfullsql);
		 Map conditionmap = new HashMap();
         conditionmap.put("orderName", orderName);
         conditionmap.put("datarange", datarange);
         conditionmap.put("orderNum", orderNum);
         conditionmap.put("viewtype", viewtype);  
         conditionmap.put("perPageCount", perPageCount);
         conditionmap.put("keyword", keyword);
         conditionmap.put("colname", colname);
         
          
         //查询现代和古代文献分别数据量
         
         DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
         List grouplist = dbo.queryList("select count(*) as filecount,type from ("+allfullsql+") group by type", null);
         System.out.println("sql:" + "select count(*) as filecount,type from ("+allfullsql+") group by type");
         if(grouplist.size() >0){
        	 for(int i =0;i<grouplist.size();i++){
        		 Map data = (Map)grouplist.get(i);
        		 String count = data.get("filecount")+"";
        		 String type = data.get("type")+"";
        		 
        		 if("2".equals(type)){
        			 if("null".equals(count)){
            			 count = "0";
            		 }
        			 modelMap.put("xiandaicount", count);
        		 }
        		 if("1".equals(type)){
        			 if("null".equals(count)){
            			 count = "0";
            		 }
        			 modelMap.put("gudaicount", count);
        		 }
        	 }
         } 
	     QueryPage qp = new QueryPage(request,request.getContextPath()+"/muti/doSearch.do",allfullsql.toString(),conditionmap); 
//	     if(null == perPageCount || "".equals(perPageCount)){
//			qp.setPerPageCount(10);
//		 }else {
//			qp.setPerPageCount(Integer.parseInt(perPageCount));
//		 }
	     qp.query();  
	     String pagenum =  qp.getCurrentPageNo() + "";
	     String pagecount = qp.getPerPageCount() + ""; 
	     modelMap.put("pagenum", pagenum);
	     modelMap.put("pagecount", pagecount);
	     modelMap.put("pagestr", qp.getPageStr());
	     modelMap.put("datalist", qp.getDataList());
	     modelMap.put("allcount", qp.getTotalcount());
	     modelMap.put("allsql", allfullsql.toString()); 
	     modelMap.put("onesql", allfullsql.toString()); 
 		 return new ModelAndView("muti/sum",modelMap);
		 
	 } 
//	   <option value="disname">中医病名</option>
//     <option value="disnamen">西医病名</option>
//     <option value="herbname">中药名称</option>
//     <option value="paraname">并发症</option>
//     <option value="point">穴位</option>
//     <option value="prename">方剂</option>
//     <option value="symname">证候</option>
//     <option value="maganame">期刊标题</option>
//     <option value="bookname">书籍名称</option>
	 private String generaterSQLold(String colname,String keyword){
		 StringBuffer buffer = new StringBuffer("");
		 if("disname".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			 
			 buffer.append("where gg.OLDDISNAME like '%").append(keyword).append("%' or ");
			 buffer.append(" (gg.MODDISNAME like '%").append(keyword).append("%' and gg.DTYPE ='1')");
		 }else if("disnamen".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			 
			 buffer.append("where  ");
			 buffer.append(" gg.MODDISNAME like '%").append(keyword).append("%' and gg.DTYPE ='2'");
		 }else if("herbname".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			
			 buffer.append(" where a.id in(select bookid from DNMS_HERB where HERBNAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("paraname".equals(colname)){
			 
		 }else if("point".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			
			 buffer.append(" where a.id in(select bookid from DNMS_POINT where POINTNAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("prename".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			 
			 buffer.append(" where a.id in(select bookid from DNMS_PRE where PRENAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("symname".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");			
			 buffer.append(" where a.id in(select bookid from DNMS_SYM where SYMPTOMNAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("maganame".equals(colname)){
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  where ");
             buffer.append(" a.BOOKNAME  like");
			 buffer.append("'%").append(keyword).append("%' "); 
		 } else  {
			 
			 buffer.append(" SELECT   1 AS TYPE,a.id,(select count(*) from dnms_file where bookid=a.id) as fcount, gg.olddisname as authorunit, gg.moddisname as title, gg.dtype as periodicalname, a.bookname AS author, a.author as keywords, a.publishhouse as YEAR, a.themewords as abstract, a.originaltext as diseasename, a.chapter as diseasenamen, a.pagenumber  as cc  from (select dis.DISEASENAME as olddisname,dis.BOOKID,d.moddisname,d.dtype  from DNMS_DISEASE dis left join (select c.disid,(select DISNAME from DNMS_MODERN_DIS b where b.DISTYPE=c.MODDISTYPE and b.id=c.modid) as moddisname,C.MODDISTYPE as dtype,C.BOOKID  from DNMS_DIS_ANC_MOD c) d on id=d.disid) gg left join DNMS_BOOK a  on gg.bookid=a.id  ");
		 }
		 return buffer.toString();
	 }
	 private String generaterSQL(String colname,String keyword){
		 StringBuffer buffer = new StringBuffer("");
		 if("disname".equals(colname)){
			 buffer.append("select 2 as type,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.DISEASENAME like ");
			 buffer.append("'%").append(keyword).append("%' ");
		 }else if("disnamen".equals(colname)){
			 buffer.append("select 2 as type,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.DISEASENAMEN like ");
			 buffer.append("'%").append(keyword).append("%' ");
		 }else if("herbname".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.id" );
			 buffer.append(" in(select disid from DNMS_SYM1 where id in ");
			 buffer.append(" (select SYMID from DNMS_PRE1 where id in "); 
			 buffer.append(" (select PREID from DNMS_HERB1 where HERBNAME like "); 
			 buffer.append("'%").append(keyword).append("%' ").append(")))");
		 }else if("paraname".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.id" );
			 buffer.append(" in(select disid from DNMS_PARADIS1 where PARADISNAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("point".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.id" );
			 
			 buffer.append(" in (select SYMID from DNMS_POINTGROUP1 where id in "); 
			 buffer.append(" (select PGROUPID from DNMS_POINT1 where POINTNAME like "); 
			 buffer.append("'%").append(keyword).append("%' ").append("))");
		 }else if("prename".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.id" );
			 buffer.append(" in(select disid from DNMS_SYM1 where id in ");
			 buffer.append(" (select SYMID from DNMS_PRE1 where PRENAME like "); 
			 buffer.append("'%").append(keyword).append("%' ").append("))");
		 }else if("symname".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id and d.id" );
			 buffer.append(" in(select disid from DNMS_SYM1 where SYMPTOMNAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(")");
		 }else if("maganame".equals(colname)){
			 buffer.append("select 2 as type ,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga left join DNMS_DISEASE1 d on d.indexid = maga.id where  " );
			 buffer.append(" maga.MAGANAME like ");
			 buffer.append("'%").append(keyword).append("%' ").append(" or ");
			 buffer.append(" maga.TITLEENG like ");
			 buffer.append("'%").append(keyword).append("%' ");
		 } else if("bookname".equals(colname)){
			  
		 }else{
			 buffer.append("select 2 as type,maga.id,(select count(*) from dnms_file1 where magaid=maga.id) as fcount,maga.AUTHORUNIT,maga.MAGANAME as title,maga.PERIODICALNAME,maga.AUTHOR,maga.KEYWORDS,maga.YEAR,maga.ABSTRACT,D.DISEASENAME,D.DISEASENAMEN,'cc' from DNMS_MAGAZINE1 maga,DNMS_DISEASE1 d where d.indexid = maga.id  ");
		 }
		 return buffer.toString();
	 }
	    @RequestMapping("/produce/downloadbookFile.do")   
		public   ModelAndView  downloadbookFile(HttpServletRequest request,
				HttpServletResponse response, ModelAndView mv) throws Exception {
				Map modelMap = new HashMap();
				String pid = request.getParameter("pid");
				String type = request.getParameter("type");
			    response.setCharacterEncoding("UTF-8");
			    GlobalParamCache cache = GlobalParamCache.getInst();
			    DBMgr dbo = this.getDbo(request);
			    
			    //根据文献类型获取文件存放路径
				//DnmsFile pobj = (DnmsFile)dataService.findByID(DnmsFile.class, new Long(pid));
			    String fileName = null;//pobj.getFilename();
			    String fileDbURL = null;//pobj.getFilepath();
			    String filesql = null;
				String filePath = null;
				if("2".equals(type)){
					filePath = GlobalParamCache.getInst().getParamByKey("dnmsfile");
					filesql = "select filename,filepath from DNMS_FILE1 where magaid=?";
				}
				if("1".equals(type)){
					filePath = GlobalParamCache.getInst().getParamByKey("dnmsabfile");
					filesql = "select filename,filepath from DNMS_FILE where bookid=?";
				}
				List filelist = dbo.queryList(filesql, new Object[]{pid});
				if(filelist.size()>0){
					Map filemap = (Map)filelist.get(0);
					fileName = (String)filemap.get("filename");
					fileDbURL = (String)filemap.get("filepath");
				}
				
				System.out.println("fileName:" + fileName);
				String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
	 			logger.debug("ext:" + ext);

	 			
	 			logger.debug("fileDbURL:" + fileDbURL);
	 			filePath += fileDbURL;
				logger.debug("test文件是否存在:" + new File(filePath).exists());

	 			filePath = filePath.replace("\\", "/");
	 			logger.debug("fileurl:" + filePath); 
				File file = new File(filePath);
				logger.debug("文件是否存在:" + file.exists());
		       
		        int fileLength = (int)file.length();
		        ServletOutputStream op       = response.getOutputStream();
		        try{
		        	int                 length   = 0;
			       
			        ServletContext      context  = request.getSession().getServletContext();
			        String              mimetype = context.getMimeType( fileName );
			        byte[]   byteContents   =   new   byte[fileLength];   
			        FileInputStream fis   =   new   FileInputStream(file);   
			        int   retcd   =   fis.read(byteContents);   
			        fis.close();    
			        logger.debug("mimetype:" + mimetype);
			        response.setContentType(mimetype == null?"application/pdf":mimetype );
			        response.setContentLength( retcd );
			        response.setHeader( "Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"),"ISO-8859-1") + "\"" );

			        op.write(byteContents,0,retcd); 
			        
		            }catch(Exception e){
		            	op.print("File not found!");
		            	op.flush();
				         
		            }finally{
		            	op.close();
		            }
	// 
	            return null;
		}
	    
	    
	    /**
		  * 现代文献数据挖掘列表
		  * @param request
		  * @param response
		  * @param modelMap
		  * @return
		  * @throws ServletException
		  * @throws IOException
		  */
		 @RequestMapping(value="/muti/dataminemodernList.do")   
	     public ModelAndView dataminemodernList(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			   
			//排序
			 StringBuffer allfullsql = new StringBuffer(); 
			 String datatype = request.getParameter("datatype");
			 String orderName = request.getParameter("orderName");
			 String orderNum = request.getParameter("orderNum"); 
			 String imageName = "";
			 StringBuffer orderByStr = new StringBuffer(" order by ");
			 if(StringUtils.isNotBlank(orderName)){
			    	if("1".equals(orderNum)){
			    		orderByStr.append(orderName).append(" asc");
			    	}
			    	if("2".equals(orderNum)){
			    		orderByStr.append(orderName).append(" desc");
			    	}
			    }else{
			    	orderByStr.append("content").append(" desc");
			    	orderName = "content";
			    	orderNum = "2";
			    }
			 request.setAttribute("orderName", orderName); 
			 request.setAttribute("orderNum", orderNum);  
			 if(null == datatype || "null".equals(datatype)){
				 datatype = "1";
				 imageName = "m_herb.png";
			 }else{
				 imageName = "m_point.png";
			 }
			 allfullsql.append("select * from DNMS_DATAMINE where DATATYPE=").append(datatype);
			 
			 allfullsql.append(orderByStr);
			 Map conditionmap = new HashMap();
	         conditionmap.put("orderName", orderName); 
	         conditionmap.put("orderNum", orderNum); 
	         conditionmap.put("datatype", datatype); 
	        
	       
		     QueryPage qp = new QueryPage(request,request.getContextPath()+"/muti/dataminemodernList.do",allfullsql.toString(),conditionmap); 

		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + ""; 
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("datatype",datatype); 
		     modelMap.put("imagename",imageName);
			 return new ModelAndView("muti/modernlist",modelMap);
			 
		 } 
		 /**
		  * 现代文献数据挖掘列表
		  * @param request
		  * @param response
		  * @param modelMap
		  * @return
		  * @throws ServletException
		  * @throws IOException
		  */
		 @RequestMapping(value="/muti/datamineoldList.do")   
	     public ModelAndView datamineoldList(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			   
			//排序
			 StringBuffer allfullsql = new StringBuffer(); 
			 String datatype = request.getParameter("datatype");
			 String orderName = request.getParameter("orderName");
			 String orderNum = request.getParameter("orderNum"); 
			 String imageName = "";
			 StringBuffer orderByStr = new StringBuffer(" order by ");
			 if(StringUtils.isNotBlank(orderName)){
			    	if("1".equals(orderNum)){
			    		orderByStr.append(orderName).append(" asc");
			    	}
			    	if("2".equals(orderNum)){
			    		orderByStr.append(orderName).append(" desc");
			    	}
			    }else{
			    	orderByStr.append("content").append(" desc");
			    	orderName = "content";
			    	orderNum = "2";
			    }
			 request.setAttribute("orderName", orderName); 
			 request.setAttribute("orderNum", orderNum);  
			 if(null == datatype || "null".equals(datatype)){
				 datatype = "1";
				 imageName = "o_herb.png";
			 }else{
				 imageName = "o_point.png";
			 }
			 allfullsql.append("select * from DNMS_DATAMINE where DATATYPE=").append(datatype);
			 
			 allfullsql.append(orderByStr);
			 Map conditionmap = new HashMap();
	         conditionmap.put("orderName", orderName); 
	         conditionmap.put("orderNum", orderNum); 
	         conditionmap.put("datatype", datatype); 
	        
	       
		     QueryPage qp = new QueryPage(request,request.getContextPath()+"/muti/dataminemodernList.do",allfullsql.toString(),conditionmap); 

		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + ""; 
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("datatype",datatype); 
		     modelMap.put("imagename",imageName);
			 return new ModelAndView("muti/oldlist",modelMap);
			 
		 } 
		 
}
